package com.zretc.support.controller;

import com.zretc.support.entity.Question;
import com.zretc.support.entity.Reply;
import com.zretc.support.entity.User;
import com.zretc.support.service.ReplyService;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api")
public class ReplyController {

    @Autowired
    private ReplyService rs;

    @Autowired
    private AjaxResponse ajaxResponse;

    @Autowired
    private PageData<Reply> pageData;

    Logger logger = LoggerFactory.getLogger(ReplyController.class);

    @PostMapping(value = "insertReply", produces = "application/json;charset=utf-8")
    public AjaxResponse insertReply(@RequestBody Reply reply) {
        logger.info("logger:" + reply);
        reply.setReplyId(0L);
        reply.setReplyTime(new Date());
        boolean flag = rs.addReply(reply);
        if (flag == true) {
            ajaxResponse.setCode(200);
            ajaxResponse.setMsg("回复成功！");
            ajaxResponse.setFlag(flag);
            return ajaxResponse;
        } else {
            ajaxResponse.setCode(0);
            ajaxResponse.setMsg("回复失败！");
            ajaxResponse.setFlag(flag);
            return ajaxResponse;
        }
    }

    @GetMapping(value = "getReply", produces = "application/json;charset=utf-8")
    public PageData<Reply> getReply(@RequestParam(value = "keywords", defaultValue = "") String keywords,
                                    @RequestParam(value = "select", defaultValue = "0") int select,
                                    @RequestParam(value = "page", defaultValue = "0") int page,
                                    @RequestParam(value = "size", defaultValue = "10") int size,
                                    @RequestParam(value = "startTime", defaultValue = "") String startTime,
                                    @RequestParam(value = "endTime", defaultValue = "") String endTime) {
        logger.info("startTime:" + startTime + ",endTime:" + endTime);
        //设置时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //赋初值
        Date sd = null;
        Date ed = null;
        //若开始的时间为空，则说明没有传值，那就查所有给个2000年的时间为初值
        if (startTime.equals("")) {
            try {
                logger.info("是否执行");
                sd = simpleDateFormat.parse("2000-01-01");
                logger.info("sd:" + sd);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            logger.info("执行");
            try {
                sd = simpleDateFormat.parse(startTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //若结束的时间为空，则说明没有传值，那就查所有给个当前时间的后一天为末值
        if (endTime.equals("")) {
            Calendar c = Calendar.getInstance();
            ed = c.getTime();
            logger.info("ed:" + ed);
        } else {
            try {
                ed = simpleDateFormat.parse(endTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        logger.info("select:" + select);
        pageData = rs.getRepliesByTime(keywords, sd, ed, page - 1, size);
        pageData.setCurrent(pageData.getCurrent() + 1);
        if (select == 1) {
            pageData = rs.getReplyByQuestionIDAndPage(Integer.parseInt(keywords), sd, ed, page-1, size);
            pageData.setCurrent(pageData.getCurrent() + 1);
        } else if (select == 2) {
            pageData = rs.getReplyByUserNameAndPage(keywords, sd, ed, page-1, size);
            pageData.setCurrent(pageData.getCurrent() + 1);
        } else if (select == 3) {
            pageData = rs.getRepliesByTime(keywords, sd, ed, page - 1, size);
            pageData.setCurrent(pageData.getCurrent() + 1);
        }
        return pageData;
    }

    @PostMapping(value = "deleteReply/{replyId}")
    public AjaxResponse deleteReply(@PathVariable int replyId) {
        logger.info("replyId:" + replyId);
        boolean flag = rs.delReplyById(replyId);
        if (flag == true) {
            ajaxResponse.setCode(200);
            ajaxResponse.setMsg("删除成功！");
            ajaxResponse.setFlag(true);
            return ajaxResponse;
        } else {
            ajaxResponse.setFlag(false);
            ajaxResponse.setMsg("删除失败！");
            ajaxResponse.setCode(0);
            return ajaxResponse;
        }
    }

    @GetMapping(value = "getReplyById/{replyId}")
    public Reply getReplyById(@PathVariable long replyId) {
        logger.info("replyId:" + replyId);
        logger.info("进来执行了吗？");
        if (replyId == 0) {
            Reply reply = new Reply();
            Question question = new Question();
            question.setQuestionId(0L);
            reply.setQuestion(question);
            User user = new User();
            user.setUserId(" ");
            user.setUserName(" ");
            reply.setUser(user);
            logger.info("reply:" + reply);
            return reply;
        }
        return rs.getReplyById(replyId);
    }

    @PutMapping(value = "updateReply", produces = "application/json;charset=utf-8")
    public AjaxResponse updateReply(@RequestBody Reply reply) {
        reply.setReplyTime(new Date());
        boolean flag = rs.updateReply(reply);
        if (flag == true) {
            ajaxResponse.setFlag(flag);
            ajaxResponse.setCode(200);
            ajaxResponse.setMsg("修改成功！");
            return ajaxResponse;
        } else {
            ajaxResponse.setMsg("修改失败！");
            ajaxResponse.setCode(0);
            ajaxResponse.setFlag(flag);
            return ajaxResponse;
        }
    }

    /**
     * 查询没有父编号回复列表
     *
     * @param questionId
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "getReWithoutPid")
    public PageData<Reply> getReplyWithoutPid(@RequestParam(value = "questionId") long questionId,
                                              @RequestParam(value = "page", defaultValue = "1") int page,
                                              @RequestParam(value = "size", defaultValue = "4") int size) {
        logger.info("questionId:" + questionId + ",page:" + page + ",size:" + size);
        pageData = rs.getReplyWithoutPid(questionId, page - 1, size);
        pageData.setCurrent(pageData.getCurrent() + 1);
        logger.info("controller" + pageData.getRecords());
        return pageData;
    }

    /**
     * 获取评论回复的回复
     * @param parentId
     * @return
     */
    @GetMapping(value = "getReplyRe")
    public List<Reply> getReplyRe(@RequestParam(value = "parentId") int parentId) {
        logger.info("parentId:"+parentId);
        return rs.getReplyRe(parentId);
    }
}
